package com.xy.iot.service.impl;
import com.xy.iot.serial.port.utils.SerialPortUtil;
import com.xy.iot.service.GnuSerialPortService;
import com.xy.iot.service.NettySerialPortRxtxClientService;
import com.xy.iot.service.SerialPortService;
import com.xy.iot.config.properties.IotSerialPortProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SerialPortServiceImpl implements SerialPortService {
    @Autowired
    private IotSerialPortProperties iotSerialPortProperties;
    @Autowired
    private GnuSerialPortService gnuSerialPortService;
    @Autowired
    private NettySerialPortRxtxClientService nettySerialPortRxtxClientService;
    @Override
    public void serialPortSendData(byte[] bytes) {
        this.initCheck();
        if (iotSerialPortProperties.getNetty()){
            nettySerialPortRxtxClientService.serialPortSendData(bytes);
        }else {
            gnuSerialPortService.serialPortSendData(bytes);
        }
    }

    @Override
    public List<String> getAllSerialPort() {
        return SerialPortUtil.findPort();
    }

    @Override
    public void initCheck() {
        if (!(iotSerialPortProperties.getOpen() || iotSerialPortProperties.getNetty())){
            throw new RuntimeException("串口参数未开启");
        }
    }
}
